//
//  ByCellShopCarListItem.m
//  oc_knowledge
//
//  Created by pro on 17/1/13.
//  Copyright © 2017年 com.itboye. All rights reserved.
//

#import "ByCellShopCarListItem.h"

@implementation ByCellShopCarListItem
{
    UIImageView *_invalidIcon;          // 失效视图

    UIButton    *_selectBtn;            // 选中视图
    UIImageView *_mainView;             // 主图
    UIView      *_sepLine;              // 两个cell之间的分割
    
    UIView      *_normalBaseView;       //
    UILabel     *_titleLabel;           // 标题
    UILabel     *_skuLabel;             // 描述
    UILabel     *_priceLabel;           // 价格
    UILabel     *_countLabel;           // 数量

    UIView      *_editingBaseView;      //
    UIButton    *_deleteBtn;            // 删除视图
    UILabel     *_countModifyLabel;     // 数量视图（可修改）
    CAShapeLayer*_lineLayer;
    UIButton    *_countAddBtn;          // 数量视图（+）
    UIButton    *_countDeleteBtn;       // 数量视图（-）
    
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        self.backgroundColor = KShopCarCellBgColor;
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        [self setupCommonUI];
        [self setupNormalUI];
        [self setupEditingUI];
    }
    return self;
}

- (void)setupCommonUI {
    
    // 失效视图
    _invalidIcon = [[UIImageView alloc] init];
    _invalidIcon.hidden = YES;
    _invalidIcon.image = [UIImage imageNamed:@"ShopCar_invalid"];
    [self.contentView addSubview:_invalidIcon];
    
    // 选中视图
    _selectBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_selectBtn setImage:[UIImage imageNamed:@"Common_unSelected"] forState:UIControlStateNormal];
    [_selectBtn setImage:[UIImage imageNamed:@"Common_selected"] forState:UIControlStateSelected];
    [_selectBtn addTarget:self action:@selector(select) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:_selectBtn];
    
    // 主图
    _mainView = [[UIImageView alloc] init];
    [self.contentView addSubview:_mainView];
    
    // 分割
    _sepLine = [[UIView alloc] init];
    _sepLine.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:_sepLine];
}
- (void)setupNormalUI {
    
    _normalBaseView = [[UIView alloc] init];
    [self.contentView addSubview:_normalBaseView];
    
    // 标题
    _titleLabel = [[UILabel alloc] init];
    _titleLabel.numberOfLines = 2;
    _titleLabel.font = FONT(14);
    _titleLabel.textColor = KShopCarCellMainTextColor;
    _titleLabel.textAlignment = NSTextAlignmentLeft;
    [_normalBaseView addSubview:_titleLabel];
    
    // 描述
    _skuLabel = [[UILabel alloc] init];
    _skuLabel.numberOfLines = 2;
    _skuLabel.adjustsFontSizeToFitWidth = YES;
    _skuLabel.font = FONT(14);
    _skuLabel.textColor = KShopCarCellSubTextColor;
    _skuLabel.textAlignment = NSTextAlignmentLeft;
    [_normalBaseView addSubview:_skuLabel];
    
    // 价格
    _priceLabel = [[UILabel alloc] init];
    _priceLabel.font = FONT(14);
    _priceLabel.textColor = KShopCarThemeColor;
    _priceLabel.textAlignment = NSTextAlignmentLeft;
    [_normalBaseView addSubview:_priceLabel];
    
    // 创建数量视图
    _countLabel = [[UILabel alloc] init];
    _countLabel.userInteractionEnabled = YES;
    _countLabel.font = FONT(14);
    _countLabel.textColor = KShopCarCellMainTextColor;
    _countLabel.textAlignment = NSTextAlignmentRight;
    [_normalBaseView addSubview:_countLabel];

}
- (void)setupEditingUI {
    
    _editingBaseView = [[UIView alloc] init];
    [self.contentView addSubview:_editingBaseView];
    
    // 选中视图
    _deleteBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _deleteBtn.backgroundColor = [UIColor redColor];
    _deleteBtn.titleLabel.font = FONT(14);
    [_deleteBtn setTitle:@"删除" forState:UIControlStateNormal];
    [_deleteBtn addTarget:self action:@selector(delete) forControlEvents:UIControlEventTouchUpInside];
    [_editingBaseView addSubview:_deleteBtn];
    
    // 创建数量修改视图
    _countModifyLabel = [[UILabel alloc] init];
    _countModifyLabel.userInteractionEnabled = YES;
    _countModifyLabel.font = FONT(14);
    _countModifyLabel.text = @"1";
    _countModifyLabel.textColor = KShopCarCellMainTextColor;
    _countModifyLabel.textAlignment = NSTextAlignmentCenter;
    [_editingBaseView addSubview:_countModifyLabel];
    _lineLayer = [[CAShapeLayer alloc] init];
    _lineLayer.strokeColor = [UIColor whiteColor].CGColor;
    [_countModifyLabel.layer insertSublayer:_lineLayer atIndex:0];
    
    _countDeleteBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _countDeleteBtn.titleLabel.font = B_FONT(25);
    [_countDeleteBtn setTitle:@"-" forState:UIControlStateNormal];
    [_countDeleteBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    [_countDeleteBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateDisabled];
    [_countDeleteBtn addTarget:self action:@selector(countDelete) forControlEvents:UIControlEventTouchUpInside];
    [_countModifyLabel addSubview:_countDeleteBtn];
    
    _countAddBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _countAddBtn.titleLabel.font = B_FONT(25);
    [_countAddBtn setTitle:@"+" forState:UIControlStateNormal];
    [_countAddBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    [_countAddBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateDisabled];
    [_countAddBtn addTarget:self action:@selector(countAdd) forControlEvents:UIControlEventTouchUpInside];
    [_countModifyLabel addSubview:_countAddBtn];
}




#pragma mark - 点击事件
- (void)select {
    
    _selectBtn.selected = !_selectBtn.selected;
    if (self.selectBlock) {
        self.selectBlock(_selectBtn.selected);
    }
}
- (void)delete {
    
    if (self.deleteBlock) {
        self.deleteBlock();
    }
}
- (void)countAdd {

    int count = _countModifyLabel.text.intValue;
    count ++;
    
    if (self.modifyBlock) {
        self.modifyBlock(count);
    }
    
}
- (void)countDelete {
    
    int count = _countModifyLabel.text.intValue;
    if (count == 1) {
        [ByCoreToolAlert showOnlyText:@"已经不能再少了!"];
        return;
    }
    
    count --;
    if (self.modifyBlock) {
        self.modifyBlock(count);
    }
}


#pragma mark - 设置属性
- (void)setIsEditing:(BOOL)isEditing {
    
    _isEditing = isEditing;
    
    _normalBaseView.hidden = _isEditing;
    _editingBaseView.hidden = !_isEditing;
}

- (void)setModel:(ByModelShopCarListItem *)model {
    
    _model = model;
    
    [self setNeedsLayout];
}

- (void)layoutSubviews {
    
    [super layoutSubviews];
    
    CGFloat margin = 5;
    CGFloat sep = 2;
    CGFloat x, y, width, height;
    // —————————— common ——————————
    // 线条
    x                       = 0;
    y                       = 0;
    width                   = self.width;
    height                  = sep;
    _sepLine.frame          = CGRectMake(x, y, width, height);

    // 失效按钮
    x                       = 0;
    y                       = 0;
    width                   = 30;
    height                  = 30 * 48 / 77.0;
    _invalidIcon.frame      = CGRectMake(x, y, width, height);
    _invalidIcon.x          = 20;
    _invalidIcon.y          = self.height / 2;
    
    // 选择按钮
    x                       = 0;
    y                       = _sepLine.bottom;
    width                   = 40;
    height                  = self.height - _sepLine.height;
    _selectBtn.frame        = CGRectMake(x, y, width, height);
    _selectBtn.selected     = _model.selected;
    CGFloat edgeH           = (_selectBtn.width - 20) / 2;
    CGFloat edgeV           = (_selectBtn.height - 20) / 2;
    [_selectBtn setImageEdgeInsets:UIEdgeInsetsMake(edgeV, edgeH, edgeV, edgeH)];
    
    // 主图
    x                       = _selectBtn.right;
    y                       = _selectBtn.top + margin;
    width                   = self.height - margin - y;
    height                  = width;
    _mainView.frame         = CGRectMake(x, y, width, height);
    [_mainView setImageWithURL:[NSURL URLWithString:_model.main_img] placeholderImage:DEFAULT_IMAGE];
    
    
    // —————————— normal ——————————
    // 基础基底视图
    x                       = _mainView.right;
    y                       = _sepLine.bottom;
    width                   = self.width - x;
    height                  = self.height - _sepLine.height;
    _normalBaseView.frame   = CGRectMake(x, y, width, height);
    
    // 标题
    x                       = 10;
    y                       = margin;
    width                   = _normalBaseView.width - 10 - x;
    height                  = [_model.name getRectOfStringWithSize:CGSizeMake(width, 10000) font:14].size.height;
    height                  = MIN(35, MAX(height, 20));
    _titleLabel.frame       = CGRectMake(x, y, width, height);
    _titleLabel.text        = _model.name;

    // 规格
    x                       = _titleLabel.left;
    y                       = _titleLabel.bottom;
    width                   = _titleLabel.width;
    if (_model.sku_desc.length == 0) {
        height              = 0;
    } else {
        height              = [_model.sku_desc getRectOfStringWithSize:CGSizeMake(width, 100000) font:14].size.height;
        height              = MIN(35, MAX(height, 20));

    }
    _skuLabel.frame         = CGRectMake(x, y, width, height);
    _skuLabel.text          = _model.sku_desc;

    // 价格
    x                       = _skuLabel.left;
    y                       = _skuLabel.bottom;
    width                   = _skuLabel.width / 2;
    height                  = 20;
    _priceLabel.frame       = CGRectMake(x, y, width, height);
    _priceLabel.text        = [NSString stringWithFormat:@"￥%.1f", _model.price.floatValue];

    // 数量
    x                       = _priceLabel.right;
    y                       = _priceLabel.top;
    width                   = _priceLabel.width;
    height                  = _priceLabel.height;
    _countLabel.frame       = CGRectMake(x, y, width, height);
    _countLabel.text        = [NSString stringWithFormat:@"x %i", _model.count.intValue];

    
    // —————————— editing ——————————
    // 编辑基底视图
    _editingBaseView.frame  = _normalBaseView.frame;
    
    // 删除按钮
    x                       = 0;
    y                       = 0;
    width                   = _editingBaseView.height / 2;
    height                  = _editingBaseView.height;
    _deleteBtn.frame        = CGRectMake(x, y, width, height);
    _deleteBtn.right        = _editingBaseView.width;
    
    // 数量编辑视图（数量）
    x                       = 0;
    y                       = 0;
    width                   = _editingBaseView.width - _deleteBtn.width;
    height                  = width / 5;
    _countModifyLabel.frame = CGRectMake(x, y, width, height);
    _countModifyLabel.text  = [NSString stringWithFormat:@"%i", _model.count.intValue];
    
    // 数量编辑视图（-）
    x                       = 0;
    y                       = 0;
    width                   = _countModifyLabel.width / 4;
    height                  = _countModifyLabel.height;
    _countDeleteBtn.frame   = CGRectMake(x, y, width, height);
    
    // 数量编辑视图（+）
    x                       = _countModifyLabel.width / 4 * 3;
    y                       = _countDeleteBtn.top;
    width                   = _countDeleteBtn.width;
    height                  = _countDeleteBtn.height;
    _countAddBtn.frame      = CGRectMake(x, y, width, height);

    // 数量编辑视图（线条）
    UIBezierPath *path      = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointMake(_countModifyLabel.width / 4, margin)];
    [path addLineToPoint:CGPointMake(_countModifyLabel.width / 4, _countModifyLabel.height - margin)];
    [path moveToPoint:CGPointMake(_countModifyLabel.width / 4 * 3, margin)];
    [path addLineToPoint:CGPointMake(_countModifyLabel.width / 4 * 3, _countModifyLabel.height - margin)];
    [path moveToPoint:CGPointMake(margin, _countModifyLabel.height)];
    [path addLineToPoint:CGPointMake(_countModifyLabel.width - margin, _countModifyLabel.height)];
    _lineLayer.path         = path.CGPath;
    
    // 设置失效商品
    if (_model.status != ByShopCarProductNormal) {
        _editingBaseView.hidden = YES;
        
        _invalidIcon.hidden     = NO;
        _selectBtn.hidden       = YES;
        
        switch (_model.status) {
            case ByShopCarProductDelete:
                _skuLabel.text  = @"商品已被删除";
                break;
            case ByShopCarProductInvalid:
                _skuLabel.text  = @"商品已失效";
                break;
            case ByShopCarProductSheleOff:
                _skuLabel.text  = @"商品已下架";
                break;
            case ByShopCarProductInventoryLack:
                _skuLabel.text  = @"商品库存不足";
                break;
            default:
                break;
        }
        
    }
    
}

@end
